local ChannelBase = import(".ChannelBase")
local ChannelTest = class("ChannelTest", ChannelBase)
local InternalLoginView = require("app.login.views.InternalLoginView")
local UserDefault = cc.UserDefault:getInstance()


function ChannelTest:ctor()
    self.super.ctor(self)
    self._account = UserDefault:getStringForKey("userAccountInternal","")
    self._isLogin = UserDefault:getBoolForKey("internalLogined",false)

    
end

function ChannelTest:getChannelTag()
	return "1"
end

function ChannelTest:getChannelId()
	return "dev"
end

function ChannelTest:_joinUrl(host)
    local channelId = self:getChannelId()
    local channelTag = self:getChannelTag()
    local session = ""
    local account = self:getAccount()
    local platform = self:getPlatform()
    assert(account,"getAccount nil")

    local url = host.."gamelogin?".."channel="..channelId

    if channelTag then
        url = url.."&channeltag="..channelTag
    end

    if session then
        url = url.."&session="..session
    end

    if account then
        url = url.."&account="..string.urlencode(account)
    end

    if platform then
        url = url.."&os="..platform
    end

    print("---------------test ChannelAPI_Test:jointUrl()",url)

    return url
end


function ChannelTest:getServerList(cb)
    -- 历史登录服务器
    -- 推荐服务器
    local gateHost = self:getGateHostFromConfig()
    local url = self:_joinUrl(gateHost)
    app.session:login(url, nil, function(decode,errorMsg,code)
        
        if cb then
            cb(decode,errorMsg,code)
        end
    end)
end



function ChannelTest:_login()
    if self:isLogin() then
        return 
    end
    if not self._account or string.len(self._account) == 0 then
        self._account = ""
        for i=1,6 do
            self._account = self._account..math.random(1,9)
        end
    end
    local view = InternalLoginView.new()
    view:setInputText(self._account)
    view:setConfirmCallback(function(sender)
    	self._account = sender:getInputText()
    	sender:removeFromParent()
    	-- local ret = "success"
		if self._account and string.len(self._account)>0 then
			
			UserDefault:setStringForKey("userAccountInternal",tostring(self._account))
            UserDefault:setBoolForKey("internalLogined",true)
            self:_onLoginSuccess()
        else
			
            -- ret = "fail"
            self:_onLoginFail()
		end

		-- if cb then
		-- 	cb(self,ret)
		-- end
    end)

    cc.Director:getInstance():getRunningScene():addChild(view,1000)
end

function ChannelTest:_logout(params)
    self._isLogin = false
    UserDefault:setBoolForKey("internalLogined",false)
    self:_onLogoutSuccess()
	-- if self.logoutCB then
	-- 	self.logoutCB(self,"success")
	-- 	self.logoutCB=nil
	-- end
end

function ChannelTest:_switchAccount(params)
    self:_logout()
end

function ChannelTest:_pay(params)
    self:_onPayFail()
end

function ChannelTest:_share(params)
    self:_onShareFail()
end





-- function ChannelTest:
return ChannelTest